package elements;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

public class Sorter {
	
	public void sortAndStore(ConcurrentLinkedQueue<Tuple> reduces) {
		Tuple tuples[] = new Tuple[reduces.size()];
		tuples = reduces.toArray(tuples);
		List<Tuple> list = Arrays.asList(tuples);
		Collections.sort(list);
		
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
			Iterator<Tuple> itr = list.iterator();
			while (itr.hasNext()) {
				Tuple tuple = itr.next();
				bw.write(tuple.v2 + " " + tuple.v1 + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
